package cn.foolishbird.hamal.frame.util;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Eric
 * @version 1.0
 * @date 2019-09-30
 */
@Slf4j
public class Utils {

    private static char[] chars = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
            'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    public static String randString(int place) {
        StringBuilder rand = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < place; i++) {
            rand.append(chars[random.nextInt(62)]);
        }
        return rand.toString();
    }

    public static String createOrderNum() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String orderNum = sdf.format(new Date());
        return orderNum + obtainRandomNumber(6);
    }

    /**
     * 首字母小写 转大写
     *
     * @param name 1
     * @return java.lang.String
     * @throws
     * @author Eric
     * @date 2019/10/6
     **/
    public static String firstAlphabetToUpper(String name) {
        char[] cs = name.toCharArray();
        if (cs[0] >= 65 && cs[0] <= 90) {
            cs[0] += 32;
        }
        return String.valueOf(cs);
    }

    /**
     * @param
     * @return java.lang.String
     * @throws
     * @author Eric
     * @date 2020/1/5
     **/
    public static String defaultObtainRandomNumber() {
        return obtainRandomNumber(6);
    }

    /**
     * @param place
     * @return java.lang.String
     * @throws
     * @author Eric
     * @date 2020/1/5
     **/
    public static String obtainRandomNumber(int place) {
        StringBuilder num = new StringBuilder();
        num.append(new Random().nextInt(obtainDecimal(place)));
        while (num.length() < place) {
            num.append("0");
        }
        return num.toString();
    }


    /**
     * 通过位置 获取到对应整数
     *
     * @param place
     * @return int
     * @throws
     * @author Eric
     * @date 2020/1/5
     **/
    public static int obtainDecimal(int place) {
        int math = 1;
        while (place > 1) {
            math *= 10;
            place--;
        }
        return math;
    }

}
